package de.hsmannheim.poolraumapp.activity;

import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import de.hsmannheim.poolraumapp.R;
import de.hsmannheim.poolraumapp.fragment.NavigationFragment;
import de.hsmannheim.poolraumapp.fragment.NavigationFragment.NavigationListener;
import de.hsmannheim.poolraumapp.fragment.ResultsFragment;
import de.hsmannheim.poolraumapp.fragment.WhatFragment;
import de.hsmannheim.poolraumapp.fragment.WhenFragment;
import de.hsmannheim.poolraumapp.fragment.WhereFragment;
import de.hsmannheim.poolraumapp.fragment.util.FragmentFactory;

/**
 * An activity representing a list of Items. This activity has different
 * presentations for handset and tablet-size devices. On handsets, the activity
 * presents a list of items, which when touched, lead to a
 * {@link DetailActivity} representing item details. On tablets, the
 * activity presents the list of items and item details side-by-side using two
 * vertical panes.
 * <p>
 * The activity makes heavy use of fragments. The list of items is a
 * {@link MenuFragment} and the item details (if present) is a
 * {@link WhenFragment}.
 * <p>
 * This activity also implements the required {@link MenuFragment.NavigationListener}
 * interface to listen for item selections.
 */
public class NavigationActivity extends PoolraumFragmentActivity implements
		NavigationListener {

	private static final String TAG = NavigationActivity.class.getName();
	
	/**
	 * Whether or not the activity is in two-pane mode, i.e. running on a tablet
	 * device.
	 */
	private boolean mTwoPane;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.d(TAG, "onCreated called");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_navigation);

		if (findViewById(R.id.detail_container) != null) {
			Log.d(TAG, "starting in two pane mode");
			// The detail container view will be present only in the
			// large-screen layouts (res/values-large and
			// res/values-sw600dp). If this view is present, then the
			// activity should be in two-pane mode.
			mTwoPane = true;

			// In two-pane mode, list items should be given the
			// 'activated' state when touched.
			((NavigationFragment) getSupportFragmentManager().findFragmentById(
					R.id.fragment_navigation)).setIsTwoPane(true);
		}
		else {
			mTwoPane = false;
			
			Log.d(TAG, "starting in single pane mode");
		}
		
		Log.d(TAG, "onCreated finished");
	}
	
	/**
	 * Callback method from {@link MenuFragment.NavigationListener} indicating that
	 * the item with the given ID was selected.
	 */
	@Override
	public void onNavigateToDetail(String id) {
		Log.d(TAG, "navigationItemSelected called with id " + id);
		
		if (mTwoPane) {
			// In two-pane mode, show the detail view in this activity by
			// adding or replacing the detail fragment using a
			// fragment transaction.
			
			Log.d(TAG, "switching detail fragment in two pane mode to  " + id);			
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.detail_container, FragmentFactory.createFragment(id)).commit();

		} else {
			// In single-pane mode, simply start the detail activity
			// for the selected item ID.
			
			Log.d(TAG, "starting detail activity for  " + id);
			
			Intent detailIntent = new Intent(this, DetailActivity.class);
			detailIntent.putExtra(DetailActivity.FRAGMENT_ID, id);
			startActivity(detailIntent);
		}
		
		Log.d(TAG, "navigationItemSelected finished");
	}
}
